﻿// E-Book Copier
// Copyright (C) 2009 Chuan-Gen Fan
// 
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
// 
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
// 
// You should have received a copy of the GNU General Public License
// along with this program.  If not, see <http://www.gnu.org/licenses/>.
// 
// Project: http://code.google.com/p/ebookcopier/
// Contact Author:
//    website: http://chuangen.name/
// e-mail/msn: chuangen@live.cn

using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
using System.Windows.Forms;

namespace EbookCopier.Properties
{
    partial class Settings
    {
        /// <summary>
        /// 获取或设置 区域热键。
        /// </summary>
        public HotkeyValue SetRectHotkey
        {
            get
            {
                int keyCode = this.SetRectHotKey_KeyCode;
                int modifiers = this.SetRectHotKey_Modifiers;
                if (keyCode == 0 && modifiers == 0)
                {//设置无效
                    return new HotkeyValue(Keys.Control, Keys.D1);
                }
                else
                {
                    return new HotkeyValue((Keys)modifiers, (Keys)keyCode);
                }
            }
            set
            {
                if (value == null)
                    return;

                this.SetRectHotKey_KeyCode =(int) value.KeyCode;
                this.SetRectHotKey_Modifiers = (int)value.Modifiers;
            }
        }
        /// <summary>
        /// 获取或设置 屏幕捕捉热键。
        /// </summary>
        public HotkeyValue CaptureHotkey
        {
            get
            {
                int keyCode = this.CaptureHotKey_KeyCode;
                int modifiers = this.CaptureHotKey_Modifiers;
                if (keyCode == 0 && modifiers  == 0)
                {//设置无效
                    return new HotkeyValue(Keys.Control, Keys.D2);
                }
                else
                {
                    return new HotkeyValue((Keys)modifiers, (Keys)keyCode);
                }
            }
            set
            {
                if (value == null)
                    return;

                this.CaptureHotKey_KeyCode = (int)value.KeyCode;
                this.CaptureHotKey_Modifiers = (int)value.Modifiers;
            }
        }


        /// <summary>
        /// 获取或设置 热键 - 开始连续捕捉。
        /// </summary>
        public HotkeyValue HotkeyStart
        {
            get
            {
                int keyCode = this.HotkeyStart_KeyCode;
                int modifiers = this.HotkeyStart_Modifiers;
                if (keyCode == 0 && modifiers == 0)
                {//设置无效
                    return new HotkeyValue(Keys.Control | Keys.Shift, Keys.A);
                }
                else
                {
                    return new HotkeyValue((Keys)modifiers, (Keys)keyCode);
                }
            }
            set
            {
                if (value == null)
                    return;

                this.HotkeyStart_KeyCode = (int)value.KeyCode;
                this.HotkeyStart_Modifiers = (int)value.Modifiers;
            }
        }

        /// <summary>
        /// 获取或设置 热键 - 暂停连续捕捉。
        /// </summary>
        public HotkeyValue HotkeyPause
        {
            get
            {
                int keyCode = this.HotkeyPause_KeyCode;
                int modifiers = this.HotkeyPause_Modifiers;
                if (keyCode == 0 && modifiers == 0)
                {//设置无效
                    return new HotkeyValue(Keys.Control | Keys.Shift, Keys.B);
                }
                else
                {
                    return new HotkeyValue((Keys)modifiers, (Keys)keyCode);
                }
            }
            set
            {
                if (value == null)
                    return;

                this.HotkeyPause_KeyCode = (int)value.KeyCode;
                this.HotkeyPause_Modifiers = (int)value.Modifiers;
            }
        }


        /// <summary>
        /// 获取或设置 热键 - 停止连续捕捉。
        /// </summary>
        public HotkeyValue HotkeyStop
        {
            get
            {
                int keyCode = this.HotkeyStop_KeyCode;
                int modifiers = this.HotkeyStop_Modifiers;
                if (keyCode == 0 && modifiers == 0)
                {//设置无效
                    return new HotkeyValue(Keys.Control | Keys.Shift, Keys.C);
                }
                else
                {
                    return new HotkeyValue((Keys)modifiers, (Keys)keyCode);
                }
            }
            set
            {
                if (value == null)
                    return;

                this.HotkeyStop_KeyCode = (int)value.KeyCode;
                this.HotkeyStop_Modifiers = (int)value.Modifiers;
            }
        }



        /// <summary>
        /// 获取或设置 数据记录文件保存目录。返回值确保文件夹存在。
        /// </summary>
        public string RecordPath
        {
            get
            {
                // make sure the folder exist
                string recordPath = this.SavedPath;
                string folderPath = "";
                if(recordPath == null || recordPath.Trim() == "")
                    folderPath = "";
                else
                    folderPath = Path.GetFullPath(recordPath);

                if (!Directory.Exists(folderPath))
                {//如果指定的不存在，则尝试创建
                    try
                    {
                        Directory.CreateDirectory(folderPath);
                    }
                    catch (Exception)
                    {//如果创建失败，则在“我的文档中保存。”
                        folderPath = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments);
                    }
                }
                return folderPath;
            }
            set
            {
                string folderPath = "";
                if (value == null || value.Trim() == "")
                    folderPath = "";
                else
                    folderPath = Path.GetFullPath(value);
                if (Directory.Exists(folderPath))
                {
                    this.SavedPath = folderPath;
                }
                else
                {//如果指定的不存在，则尝试创建
                    try
                    {
                        Directory.CreateDirectory(folderPath);
                        this.SavedPath = folderPath;
                    }
                    catch (Exception)
                    {//如果创建失败，则不设置
                        ;
                    }
                }
            }
        }

        /// <summary>
        /// 记录命名规则。%year%-%month%-%day% %count%.rec
        /// </summary>
        public string RecordFileNamedRule = "%year%-%month%-%day% %count%";//"%year%-%month%-%day%_%hour%%min%%sec%%ms%";
        /// <summary>
        /// 根据命名规则生成一个新的记录文件名称。
        /// </summary>
        public string GetNewRecordFileName(DateTime datetime, string ext)
        {
            return GetNewRecordFileName(RecordFileNamedRule, this.RecordPath, datetime, ext);
        }
        /// <summary>
        /// 根据命名规则生成一个新的记录文件名称。
        /// </summary>
        public static string GetNewRecordFileName(string namedRule, string parentPath, DateTime datetime, string ext)
        {
            string result = namedRule + ext;
            result = result.Replace("%year%", string.Format("{0:0000}", datetime.Year));
            result = result.Replace("%month%", string.Format("{0:00}", datetime.Month));
            result = result.Replace("%day%", string.Format("{0:00}", datetime.Day));
            result = result.Replace("%hour%", string.Format("{0:00}", datetime.Hour));
            result = result.Replace("%min%", string.Format("{0:00}", datetime.Minute));
            result = result.Replace("%sec%", string.Format("{0:00}", datetime.Second));
            result = result.Replace("%ms%", string.Format("{0:000}", datetime.Millisecond));

            if (result.IndexOf("%count%") >= 0)
            {//存在 %count%
                if (Directory.Exists(parentPath))
                {//存在目录，则返回一个未被占用的文件名
                    string tmpName = "";
                    for (int i = 0; i < 10000; i++)
                    {
                        tmpName = result.Replace("%count%", string.Format("{0:0000}", i));
                        if (!File.Exists(Path.Combine(parentPath, tmpName)))
                        {//不存在
                            result = tmpName;
                            break;
                        }
                    }
                }
                else
                {
                    result = result.Replace("%count%", "0000");
                }
            }

            return result;
        }
    }
}
